'use strict';

app.factory("appShopAccount", ["Shop", "shopAccountStore", "connectPro", function (Shop, shopAccountStore, connectPro) {
    function AppShopAccount(options) {
        Shop.call(this, {
            request: {
                url: "/api/shop/full"
            },
            store: shopAccountStore
        });
    };

    AppShopAccount.prototype = Object.create(Shop.prototype);
    AppShopAccount.prototype.constructor = AppShopAccount;

    AppShopAccount.prototype.disconnect = function () {
        return connectPro.disconnect();
    }

    return new AppShopAccount();
} ]);


app.factory("appShopNews", ["AlertPaging", "shopNewsStore", "Alert", function (AlertPaging, shopNewsStore, Alert) {
    return new AlertPaging({
        request: {
            url: "/api/alert/subscriber/shop"
        },
        store: shopNewsStore,
        groupBy: "data.dates.creation | date:'dd/MM/yyyy'",
        remove: {
            url: "/api/alert/shop/delete",
            request: {
                key: "alertId",
                value: "item._id"
            }
        },
        singleApp: {
            type: Alert,
            options: {}
        }
    });
} ]);

app.factory("appShopAlive", ["AlertPaging", "shopAliveStore", "Alert", function (AlertPaging, shopAliveStore, Alert) {
    return new AlertPaging({
        request: {
            url: "/api/alert/shop"
        },
        store: shopAliveStore,
        groupBy: "data.dates.creation | date:'dd/MM/yyyy'",
        remove: {
            url: "/api/alert/shop/delete",
            request: {
                key: "alertId",
                value: "item._id"
            }
        },
        singleApp: {
            type: Alert,
            options: {}
        }
    });

} ]);

app.factory("appShopArchives", ["$http", "$location", "$timeout", "$token", "$canceler", "Alert", function ($http, $location, $timeout, $token, $canceler, Alert) {
    function AppShopArchives() {
        this.value = null;
        this.items = [];
        this.skip = 0;
    };

    AppShopArchives.prototype.reset = function (value) {
        this.value = value;
        this.items = [];
        this.skip = 0;
    };

    AppShopArchives.prototype.select = function () {
        //$location.path("/search/" + this.value);
        //console.log("appShopArchives select", this.value);
        this.reset(this.value);
    };

    AppShopArchives.prototype.research = function (value) {
        //console.log("appShopArchives research", value);
        return $http.get("api/research", { params: { q: value, limit: 10 }, timeout: $canceler.create().promise }).then(function (res) {
            return res.data;
        }).catch(function (error) {
            $exception.show(error);
        });
    };

    AppShopArchives.prototype.init = function (value) {
        this.reset(value);
        return this.paging();
    };

    AppShopArchives.prototype.paging = function () {
        var self = this;
        return $token.get().then(function (token) {
            return $http.get("api/archives/alert", { params: { token: token, q: self.value, skip: self.skip, limit: 3 }, timeout: $canceler.create().promise }).then(function (res) {

                res.data.forEach(function (item) {
                    var object = self.createProvider(item);
                    self.items.push(object);
                });

                self.skip = self.items.length;
            });
        }).catch(function (error) {
            $exception.show(error);
        });
    };

    AppShopArchives.prototype.createProvider = function (data) {
        if(data.type == "alert") return new Alert({data: data.obj});
    };

    return new AppShopArchives();
} ]);

app.factory("appShopSubscription", ["SubscriptionPaging", "shopSubscriptionStore", "Shop", function (SubscriptionPaging, shopSubscriptionStore, Shop) {
    
    return new SubscriptionPaging({
        request: {
            url: "/api/shop/subscriptions"
        },
        store: shopSubscriptionStore,
        groupBy: "data.login | firstLetter",
        remove: {
            url: "/api/shop/unsubscribe/shop",
            request: {
                key: "shopId",
                value: "item._id"
            }
        },
        singleApp: {
            type: Shop,
            options: {}
        }
    });
} ]);

app.factory("appAlertArchives", ["MultiData", "Alert", function (MultiData, Alert) {
    return new MultiData({
        request: {
            url: "/api/alert/archive" /* key is append */
        },
        persistence: false,
        type: Alert
    });
} ]);

